package com.ruoyi.web.controller.basic;

import com.ruoyi.basic.domain.*;
import com.ruoyi.basic.service.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.TreeUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Controller
@RequestMapping("/category/detailreal")
public class TbDailyUnitPriceRealController extends BaseController
{
    private String prefix = "basic/price/real";

    @Autowired
    private ITbDailyUnitPriceService tbDailyUnitPriceService;
    @Autowired
    private ITbCategoryRealService categoryRealService;
    @Autowired
    private ITbDailyUnitPriceDetailRealService dailyUnitPriceDetailRealService;


    @RequiresPermissions("category:detailreal:view")
    @GetMapping("real")
    public String real(ModelMap mmap)
    {
        mmap.put("type",2);
        return prefix + "/price";
    }

    /**
     * 实际工日单价列表
     */
    @RequiresPermissions("category:detailreal:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TbDailyUnitPrice tbDailyUnitPrice)
    {
        startPage();
        List<TbDailyUnitPrice> list = tbDailyUnitPriceService.listByCondition(tbDailyUnitPrice);
        return getDataTable(list);
    }

    /**
     * 实际工日单价列表
     */
    @RequiresPermissions("category:detailreal:export")
    @Log(title = "实际工日单价", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TbDailyUnitPrice tbDailyUnitPrice)
    {
        List<TbDailyUnitPrice> list = tbDailyUnitPriceService.listByCondition(tbDailyUnitPrice);
        ExcelUtil<TbDailyUnitPrice> util = new ExcelUtil<TbDailyUnitPrice>(TbDailyUnitPrice.class);
        return util.exportExcel(list, "实际工日单价数据");
    }

    /**
     * 实际工日单价
     */
    @GetMapping("/real/add")
    public String add(ModelMap mmap)
    {
        TbDailyUnitPrice last = tbDailyUnitPriceService.getLast(2);
        List<TbCategoryReal> categories = categoryRealService.listByCondition(new TbCategoryReal());
        if(ObjectUtils.isNotEmpty(last)){
            List<TbDailyUnitPriceDetailReal> detailList = dailyUnitPriceDetailRealService.listByUnitPriceId(last.getId());
            Map<Long, TbDailyUnitPriceDetailReal> dailyUnitPriceDetailMap = detailList.stream().collect(Collectors.toMap(TbDailyUnitPriceDetailReal::getCategoryId, i -> i, (k1, k2) -> k1));
            DecimalFormat df = new DecimalFormat("#0.00");
            categories.forEach(item->{
                if(dailyUnitPriceDetailMap.containsKey(item.getId())){
                    TbDailyUnitPriceDetailReal tbDailyUnitPriceDetail = dailyUnitPriceDetailMap.get(item.getId());
                    item.setAvgPrice(new BigDecimal(df.format(tbDailyUnitPriceDetail.getAvgPrice())));
                    item.setUnitPriceId(tbDailyUnitPriceDetail.getUnitPriceId());
                    item.setDetailId(tbDailyUnitPriceDetail.getId());
                }
            });
        }
        List<TbCategoryReal> tbCategories = TreeUtils.generateTrees(categories);
        mmap.put("categorys",tbCategories);
        return prefix + "/add";
    }

    /**
     * 实际工日单价
     */
    @RequiresPermissions("category:detailreal:add")
    @Log(title = "实际工日单价", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated @RequestBody TbDailyUnitPrice tbDailyUnitPrice)
    {
        return toAjax(tbDailyUnitPriceService.saveDailyUnitPrice(tbDailyUnitPrice));
    }

    /**
     * 修改实际工日单价
     */
    @RequiresPermissions("category:detailreal:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        TbDailyUnitPrice tbDailyUnitPrice = tbDailyUnitPriceService.getById(id);
        mmap.put("dailyUnitPrice", tbDailyUnitPrice);
        List<TbDailyUnitPriceDetailReal> detailList = dailyUnitPriceDetailRealService.listByUnitPriceId(id);
        List<TbCategoryReal> categories = categoryRealService.listByCondition(new TbCategoryReal());
        Map<Long, TbDailyUnitPriceDetailReal> dailyUnitPriceDetailMap = detailList.stream().collect(Collectors.toMap(TbDailyUnitPriceDetailReal::getCategoryId, i -> i, (k1, k2) -> k1));
        DecimalFormat df = new DecimalFormat("#0.00");
        categories.forEach(item->{
            if(dailyUnitPriceDetailMap.containsKey(item.getId())){
                TbDailyUnitPriceDetailReal tbDailyUnitPriceDetail = dailyUnitPriceDetailMap.get(item.getId());
                if (tbDailyUnitPriceDetail.getAvgPrice() != null) {
                    item.setAvgPrice(new BigDecimal(df.format(tbDailyUnitPriceDetail.getAvgPrice())));
                }
                item.setUnitPriceId(tbDailyUnitPriceDetail.getUnitPriceId());
                item.setDetailId(tbDailyUnitPriceDetail.getId());
            }
        });
        List<TbCategoryReal> tbCategories = TreeUtils.generateTrees(categories);
        mmap.put("categorys",tbCategories);
        return prefix + "/edit";
    }

    /**
     * 修改保存定额工日/实际工日单价
     */
    @RequiresPermissions("category:detailreal:edit")
    @Log(title = "实际工日单价", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated @RequestBody TbDailyUnitPrice tbDailyUnitPrice)
    {
        return toAjax(tbDailyUnitPriceService.updateDailyUnitPrice(tbDailyUnitPrice));
    }

    /**
     * 删除定额工日/实际工日单价
     */
    @RequiresPermissions("category:detailreal:remove")
    @Log(title = "定额工日/实际工日单价", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(tbDailyUnitPriceService.deleteByIds(ids));
    }

    /**
     * 查询定额工日/实际工日单价列表
     */
    @RequiresPermissions("category:detailreal:list")
    @PostMapping("/detail/list")
    @ResponseBody
    public TableDataInfo detailList(TbDailyUnitPrice tbDailyUnitPrice)
    {
        startPage();
        List<TbDailyUnitPriceDetailReal> list = dailyUnitPriceDetailRealService.listByCondition(tbDailyUnitPrice);
        return getDataTable(list);
    }


    @PostMapping("/checkUnitPriceDateUnique")
    @ResponseBody
    public String checkUnitPriceDateUnique(TbDailyUnitPrice tbDailyUnitPrice)
    {
        return tbDailyUnitPriceService.checkUnitPriceDateUnique(tbDailyUnitPrice);
    }

    @GetMapping("/select")
    public String select(Long id, ModelMap mmap)
    {
        String url = prefix + "/select-dialog";
        mmap.put("id",id);
        return url;
    }

}
